﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using PagedList;
 

namespace Freshbox2._0.UI.Controllers
{
    public class AreaLoginController : BaseController<arealog>
    {
        //
        // GET: /AreaLogin/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<arealog> bll
        {
            get
            {
                return new AreaLogBll();
            }

        }
        public class ListLogModel  // logId  logTime  logType  waresCode     status   RealName   userid  storeName    waresName num
        {
            public int logId;
            public string logTime;
            public string waresCode;
            public string logType;
            public string waresName;
            public string RealName;
            public int userid;
            public string storeName;
            public string logReason;
            public int? status;
            public int num;

        }

        ManagerAreaBll managerAreaBll = new ManagerAreaBll();

        AreaBll areaBll = new AreaBll();
                                                                                                                                    
        WaresBll waresBll = new WaresBll();

        //public ActionResult GetNotice(int page, int rows, arealog model, string date1, string date2)
        //{
                
        //    var listLogModel = new List<ListLogModel>();

        //    //1. 查询出意见表表里的所有的信息                           
        //    Expression<Func<arealog, bool>> condition1 = x => x.logType == 0;

        //    if (string.IsNullOrEmpty(date1) || string.IsNullOrEmpty(date2))
        //    {
        //        // date1 date2为空，代表第一次查询 或者是没有选日期的查询，默认选查询所有的数据
        //        condition1 = x => x.logType == 0;
        //    }
        //    else
        //    {
        //        // 1.先筛选出这个时间段的 满足要求的信息   
        //        DateTime datetime1 = DateTime.Parse(date1);
        //        DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
        //        condition1 = x => x.logTime >= datetime1 && x.logTime <= datetime2 && x.logType == 0;
        //    }
        //    var listNotice = bll.Search().Where(condition1).Select(x => new
        //    {
        //        x.logId,
        //        x.managerAreaCode,
        //        x.status,
        //        x.logTime,
        //        logType = x.logType == 1 ? "上架" : "下架",
        //        x.areaCode,
        //        x.waresCode,
        //        x.logReason,
        //        x.afterUpdate,
        //        x.beforeUpdate
        //    }).ToList();

        //    foreach (var oneCode in listNotice)
        //    {
        //        int managerid = Convert.ToInt32(oneCode.managerAreaCode);

        //        //根据订单详情表里的商品编号  进行查询用户表的相关信息 

        //        var list2 = managerAreaBll.Search(x => x.areaMaID == managerid).Select(x => new { x.name, x.areaMaID, }).ToList();

        //        var storeList = areaBll.Search(x => x.areaCode == oneCode.areaCode).Select(x => new { x.areaCode, x.areaName, }).ToList();

        //        var wareList = waresBll.Search(x => x.waresCode == oneCode.waresCode).Select(x => new { x.waresCode, x.waresName, }).ToList();

        //        //判断 驳回的操作是否被理货员处理  >=1 代表已经处理
        //        int isRun = bll.Search(x => x.managerAreaCode == oneCode.managerAreaCode && x.logType == 1 && x.waresCode == oneCode.waresCode && x.areaCode == oneCode.areaCode && x.logTime >= oneCode.logTime).Count();


        //        if (list2.Count != 0 && storeList.Count != 0 && wareList.Count() != 0)
        //        {
        //            ListLogModel logmodel = new ListLogModel();
        //            logmodel.logId = oneCode.logId;
        //            logmodel.logType = oneCode.logType;   //double aaaaa = Math.abs(你的变量);  求绝对值 

        //            logmodel.num = Math.Abs(Convert.ToInt32(oneCode.beforeUpdate) - Convert.ToInt32(oneCode.afterUpdate));
        //            if (isRun >= 1)
        //            {
        //                logmodel.status = 2;
        //            }
        //            else
        //            {       
        //                logmodel.status = oneCode.status;
        //            }
        //            logmodel.waresCode = wareList.FirstOrDefault().waresCode;
        //            logmodel.waresName = wareList.FirstOrDefault().waresName;
        //            logmodel.storeName = storeList.FirstOrDefault().areaName;
        //            logmodel.userid = list2.FirstOrDefault().areaMaID;
        //            logmodel.logReason = oneCode.logReason;
        //            logmodel.RealName = list2.FirstOrDefault().name ;
        //            logmodel.logTime = Convert.ToDateTime(oneCode.logTime).ToString("yyyy-MM-dd HH:mm:ss");
        //            listLogModel.Add(logmodel);
        //        }   
        //    }

        //    var listNotice2 = bll.Search(x => x.IsRead == 0).Where(condition1).Select(x => new
        //    {
        //        x.logId,
        //        x.managerAreaCode,
        //        x.status,
        //        x.logTime,
        //        logType = x.logType,
        //        x.areaCode,
        //        x.waresCode,
        //        x.logReason,
        //        x.afterUpdate,
        //        x.beforeUpdate
        //    }).ToList();
        //    foreach (var item in listNotice2)
        //    {
        //        arealog model1 = new arealog();
        //        model1.logId = item.logId;
        //        model1.managerAreaCode = item.managerAreaCode;
        //        int logtype = Convert.ToInt32(item.logType);
        //        model1.logType = 0;
        //        model1.logTime = item.logTime;

        //        model1.waresCode = item.waresCode;
        //        model1.logReason = item.logReason;

        //        model1.afterUpdate = item.afterUpdate;
        //        model1.beforeUpdate = item.beforeUpdate;
        //        model1.status = item.status;

        //        model1.areaCode = item.areaCode;
        //        model1.IsRead = 1;//已读
        //        if (bll.Modify(model1) > 0)
        //        {

        //        }
        //    }

        //    var listLog = listLogModel.ToPagedList(page, rows).ToList();
        //    return Json(new { rows = listLog, total = listLogModel.Count() });
        //}

        PassMessageBll passMessageBll = new PassMessageBll();


        /// <summary>
        /// 驳回消息
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        //public ActionResult Reject(int logId, int userid, string RealName, string waresName, string waresCode, string logType, int num, string storeName, passmessage contentModel)
        //{
        //    string msg = "";
        //    int result = 0;

        //    contentModel.passTime = DateTime.Now;          
        //    DateTime? date = bll.Search(x => x.logId == logId).Select(x => new { x.logTime }).FirstOrDefault().logTime;
        //    contentModel.content = RealName + " 采销人员,您" + date + "" + logType + "  的" + storeName + "区域的，" + "商品编号为：" + waresCode + ",商品名称为:" + waresName + "的商品，该操作被管理员驳回，请你及时进行修改，如有疑问，请联系管理员。";
        //    contentModel.fromManager = 1;//(int?)CurrentUer.adminId;
        //    contentModel.toManager = userid;
            
        //    //先在 passMessage  驳回信息 表里面添加记录      
        //    if (passMessageBll.Add(contentModel) > 0)
        //    {
        //       //在修改下架记录表里的  驳回状态         
        //        arealog logModel = new arealog();
        //        var list = bll.Search(x => x.logId == logId).Select(x => new { x.logId, x.logTime, x.logType, x.logReason, x.waresCode, x.managerAreaCode, x.status, x.areaCode, x.afterUpdate, x.beforeUpdate }).ToList();
        //        logModel.logId = logId;
        //        logModel.logTime = list.FirstOrDefault().logTime;
        //        logModel.logType = list.FirstOrDefault().logType;
        //        logModel.logReason = list.FirstOrDefault().logReason;
        //        logModel.waresCode = list.FirstOrDefault().waresCode;
        //        logModel.managerAreaCode = list.FirstOrDefault().managerAreaCode;
        //        logModel.status = 0;            //    0: 已驳回，等待处理    1：点击驳回
        //        logModel.areaCode = list.FirstOrDefault().areaCode;
        //        logModel.afterUpdate = list.FirstOrDefault().afterUpdate;
        //        logModel.beforeUpdate = list.FirstOrDefault().beforeUpdate;
        //        ////  修改 驳回 状态 
        //        if (bll.Modify(logModel) > 0)
        //        {
        //            msg = "驳回成功";
        //            result = 1;
        //        }

                

        //    }
        //    return Json(new { state = result, msg = msg });
        //}
    
    
    
    }
}
